package lc.q101_150;

import java.util.ArrayList;
import java.util.List;

public class ListNode {
    public int val;
    public ListNode next;
    public ListNode() {}
    public ListNode(int val) { this.val = val; }
    public ListNode(int val, ListNode next) { this.val = val; this.next = next; }

    @Override
    public String toString() {
        List<Integer> integers = new ArrayList<>();
        ListNode head = this;
        while (head != null) {
            integers.add(head.val);
            head = head.next;
        }
        return integers.toString();
    }

    public static ListNode buildByArr(int[] arrs) {
        ListNode head = null;
        ListNode tail = new ListNode();
        for (int arr : arrs) {
            tail.next = new ListNode(arr, tail.next);
            if (head == null) {
                head = tail.next;
            }
            tail = tail.next;
        }
        return head;
    }
}
